package com.gitee.wsl.security.signature


enum class KeyType {

    /**
     * RSA is an enum for the supported RSA key type
     */
    RSA,

    /**
     * Ed25519 is an enum for the supported Ed25519 key type
     */
    ED25519,

    /**
     * Secp256k1 is an enum for the supported Secp256k1 key type
     */
    SECP256K1,

    /**
     * ECDSA is an enum for the supported ECDSA key type
     */
    ECDSA
}

interface Key {

    val keyType: KeyType

    /**
     * Bytes returns a serialized, storeable representation of this key.
     */
    fun bytes(): ByteArray

    fun raw(): ByteArray
}

/**
 * PrivKey represents a private key that can be used to generate a public key,
 * sign data, and decrypt data that was encrypted with a public key.
 */
abstract class PrivKey(override val keyType: KeyType) : Key {

    /**
     * Cryptographically sign the given bytes.
     */
    abstract fun sign(data: ByteArray): ByteArray

    /**
     * Return a public key paired with this private key.
     */
    abstract fun publicKey(): PubKey

    //override fun bytes(): ByteArray = marshalPrivateKey(this)

    override fun equals(other: Any?): Boolean {
        if (this === other || other == null) return true
        if (this::class != other::class) return false
        return bytes().contentEquals((other as PrivKey).bytes())
    }

    override fun hashCode(): Int {
        return raw().contentHashCode()
    }
}

/**
 * PubKey is a public key.
 */
abstract class PubKey(override val keyType: KeyType) : Key {

    /**
     * Verify that 'sig' is the signed hash of 'data'.
     */
    abstract fun verify(data: ByteArray, signature: ByteArray): Boolean

    //override fun bytes(): ByteArray = marshalPublicKey(this)

    override fun equals(other: Any?): Boolean {
        if (this === other || other == null) return true
        if (this::class != other::class) return false
        return bytes().contentEquals((other as PubKey).bytes())
    }

    override fun hashCode(): Int {
        return raw().contentHashCode()
    }
}